'use strict';

const { generateSnowflakeId } = require('../../app/utils/snowflake');

/** @type {import('sequelize-cli').Migration} */
module.exports = {
  async up(queryInterface, Sequelize) {
    // 获取超级管理员角色编码
    const adminRoleCode = await queryInterface.rawSelect('sys_roles', {
      where: { name: '超级管理员' },
    }, ['code']);

    // 生成顶级菜单ID
    const systemMenuId = generateSnowflakeId();
    const dashboardMenuId = generateSnowflakeId();
    
    // 插入顶级菜单
    const topLevelMenus = [
      // 系统管理目录
      {
        id: systemMenuId,
        parent_id: 0,
        name: '系统管理',
        path: '/system',
        component: null,
        icon: 'Tools',
        type: 1,
        permission: 'system:view',
        sort: 2,
        visible: true,
        status: true,
        create_time: new Date(),
        update_time: new Date(),
      },
      // 仪表盘菜单
      {
        id: dashboardMenuId,
        parent_id: 0,
        name: '仪表盘',
        path: '/dashboard',
        component: 'Dashboard',
        icon: 'DataAnalysis',
        type: 2,
        permission: 'dashboard:view',
        sort: 1,
        visible: true,
        status: true,
        create_time: new Date(),
        update_time: new Date(),
      },
    ];

    await queryInterface.bulkInsert('sys_menus', topLevelMenus);

    // 获取系统管理目录ID
    const systemMenu = systemMenuId;

    // 生成子菜单ID
    const userMenuId = generateSnowflakeId();
    const roleMenuId = generateSnowflakeId();
    const menuManagementId = generateSnowflakeId();
    const departmentMenuId = generateSnowflakeId();
    const systemConfigMenuId = generateSnowflakeId();
    const dictMenuId = generateSnowflakeId();
    const imageMenuId = generateSnowflakeId();
    
    // 插入系统管理下的子菜单
    const systemSubMenus = [
      // 用户管理菜单
      {
        id: userMenuId,
        parent_id: systemMenu,
        name: '用户管理',
        path: '/users',
        component: 'management/UserManagement',
        icon: 'user',
        type: 2,
        permission: 'user:view',
        sort: 2,
        visible: true,
        status: true,
        create_time: new Date(),
        update_time: new Date(),
      },
      // 角色管理菜单
      {
        id: roleMenuId,
        parent_id: systemMenu,
        name: '角色管理',
        path: '/roles',
        component: 'management/RoleManagement',
        icon: 'FolderOpened',
        type: 2,
        permission: 'role:view',
        sort: 2,
        visible: true,
        status: true,
        create_time: new Date(),
        update_time: new Date(),
      },
      // 菜单管理菜单
      {
        id: menuManagementId,
        parent_id: systemMenu,
        name: '菜单管理',
        path: '/menus',
        component: 'management/MenuManagement',
        icon: 'menu',
        type: 2,
        permission: 'menu:view',
        sort: 3,
        visible: true,
        status: true,
        create_time: new Date(),
        update_time: new Date(),
      },
      // 部门管理菜单
      {
        id: departmentMenuId,
        parent_id: systemMenu,
        name: '部门管理',
        path: '/departments',
        component: 'management/DepartmentManagement',
        icon: 'Connection',
        type: 2,
        permission: 'department:view',
        sort: 4,
        visible: true,
        status: true,
        create_time: new Date(),
        update_time: new Date(),
      },
      // 系统配置菜单
      {
        id: systemConfigMenuId,
        parent_id: systemMenu,
        name: '系统配置',
        path: '/config',
        component: 'management/SystemConfig',
        icon: 'Brush',
        type: 2,
        permission: 'config:view',
        sort: 5,
        visible: true,
        status: true,
        create_time: new Date(),
        update_time: new Date(),
      },
      // 字典管理菜单
      {
        id: dictMenuId,
        parent_id: systemMenu,
        name: '字典管理',
        path: '/dict',
        component: 'management/DictManagement',
        icon: 'Notebook',
        type: 2,
        permission: 'dict:view',
        sort: 6,
        visible: true,
        status: true,
        create_time: new Date(),
        update_time: new Date(),
      },
      // 图片管理菜单
      {
        id: imageMenuId,
        parent_id: systemMenu,
        name: '图片管理',
        path: '/images',
        component: 'management/ImageManagement',
        icon: 'picture',
        type: 2,
        permission: 'image:view',
        sort: 7,
        visible: true,
        status: true,
        create_time: new Date(),
        update_time: new Date(),
      },
    ];

    await queryInterface.bulkInsert('sys_menus', systemSubMenus);

    // 获取各个子菜单的ID
    const userMenu = userMenuId;
    const roleMenu = roleMenuId;
    const menuManagement = menuManagementId;
    const departmentMenu = departmentMenuId;

    // 生成按钮权限ID
    const userListId = generateSnowflakeId();
    const userCreateId = generateSnowflakeId();
    const userUpdateId = generateSnowflakeId();
    const userDeleteId = generateSnowflakeId();
    const roleListId = generateSnowflakeId();
    const roleCreateId = generateSnowflakeId();
    const roleUpdateId = generateSnowflakeId();
    const roleDeleteId = generateSnowflakeId();
    const menuCreateId = generateSnowflakeId();
    const menuUpdateId = generateSnowflakeId();
    const menuDeleteId = generateSnowflakeId();
    const departmentListId = generateSnowflakeId();
    const departmentCreateId = generateSnowflakeId();
    const departmentUpdateId = generateSnowflakeId();
    const departmentDeleteId = generateSnowflakeId();
    
    // 插入按钮权限
    const buttonPermissions = [
      // 用户管理按钮权限
      {
        id: userListId,
        parent_id: userMenu,
        name: '用户查询',
        path: null,
        component: null,
        icon: null,
        type: 3,
        permission: 'user:list',
        sort: 1,
        visible: true,
        status: true,
        create_time: new Date(),
        update_time: new Date(),
      },
      {
        id: userCreateId,
        parent_id: userMenu,
        name: '用户新增',
        path: null,
        component: null,
        icon: null,
        type: 3,
        permission: 'user:create',
        sort: 2,
        visible: true,
        status: true,
        create_time: new Date(),
        update_time: new Date(),
      },
      {
        id: userUpdateId,
        parent_id: userMenu,
        name: '用户修改',
        path: null,
        component: null,
        icon: null,
        type: 3,
        permission: 'user:update',
        sort: 3,
        visible: true,
        status: true,
        create_time: new Date(),
        update_time: new Date(),
      },
      {
        id: userDeleteId,
        parent_id: userMenu,
        name: '用户删除',
        path: null,
        component: null,
        icon: null,
        type: 3,
        permission: 'user:delete',
        sort: 4,
        visible: true,
        status: true,
        create_time: new Date(),
        update_time: new Date(),
      },
      // 角色管理按钮权限
      {
        id: roleListId,
        parent_id: roleMenu,
        name: '角色查询',
        path: null,
        component: null,
        icon: null,
        type: 3,
        permission: 'role:list',
        sort: 1,
        visible: true,
        status: true,
        create_time: new Date(),
        update_time: new Date(),
      },
      {
        id: roleCreateId,
        parent_id: roleMenu,
        name: '角色新增',
        path: null,
        component: null,
        icon: null,
        type: 3,
        permission: 'role:create',
        sort: 2,
        visible: true,
        status: true,
        create_time: new Date(),
        update_time: new Date(),
      },
      {
        id: roleUpdateId,
        parent_id: roleMenu,
        name: '角色修改',
        path: null,
        component: null,
        icon: null,
        type: 3,
        permission: 'role:update',
        sort: 3,
        visible: true,
        status: true,
        create_time: new Date(),
        update_time: new Date(),
      },
      {
        id: roleDeleteId,
        parent_id: roleMenu,
        name: '角色删除',
        path: null,
        component: null,
        icon: null,
        type: 3,
        permission: 'role:delete',
        sort: 4,
        visible: true,
        status: true,
        create_time: new Date(),
        update_time: new Date(),
      },
      // 菜单管理按钮权限
      {
        id: menuCreateId,
        parent_id: menuManagement,
        name: '菜单新增',
        path: null,
        component: null,
        icon: null,
        type: 3,
        permission: 'menu:create',
        sort: 2,
        visible: true,
        status: true,
        create_time: new Date(),
        update_time: new Date(),
      },
      {
        id: menuUpdateId,
        parent_id: menuManagement,
        name: '菜单修改',
        path: null,
        component: null,
        icon: null,
        type: 3,
        permission: 'menu:update',
        sort: 3,
        visible: true,
        status: true,
        create_time: new Date(),
        update_time: new Date(),
      },
      {
        id: menuDeleteId,
        parent_id: menuManagement,
        name: '菜单删除',
        path: null,
        component: null,
        icon: null,
        type: 3,
        permission: 'menu:delete',
        sort: 4,
        visible: true,
        status: true,
        create_time: new Date(),
        update_time: new Date(),
      },
      // 部门管理按钮权限
      {
        id: departmentListId,
        parent_id: departmentMenu,
        name: '部门查询',
        path: null,
        component: null,
        icon: null,
        type: 3,
        permission: 'department:list',
        sort: 1,
        visible: true,
        status: true,
        create_time: new Date(),
        update_time: new Date(),
      },
      {
        id: departmentCreateId,
        parent_id: departmentMenu,
        name: '部门新增',
        path: null,
        component: null,
        icon: null,
        type: 3,
        permission: 'department:create',
        sort: 2,
        visible: true,
        status: true,
        create_time: new Date(),
        update_time: new Date(),
      },
      {
        id: departmentUpdateId,
        parent_id: departmentMenu,
        name: '部门修改',
        path: null,
        component: null,
        icon: null,
        type: 3,
        permission: 'department:update',
        sort: 3,
        visible: true,
        status: true,
        create_time: new Date(),
        update_time: new Date(),
      },
      {
        id: departmentDeleteId,
        parent_id: departmentMenu,
        name: '部门删除',
        path: null,
        component: null,
        icon: null,
        type: 3,
        permission: 'department:delete',
        sort: 4,
        visible: true,
        status: true,
        create_time: new Date(),
        update_time: new Date(),
      },
    ];

    await queryInterface.bulkInsert('sys_menus', buttonPermissions);

    // 获取所有菜单ID
    const menuIds = [
      systemMenuId, dashboardMenuId, userMenuId, roleMenuId, menuManagementId, 
      departmentMenuId, systemConfigMenuId, dictMenuId, imageMenuId,
      userListId, userCreateId, userUpdateId, userDeleteId,
      roleListId, roleCreateId, roleUpdateId, roleDeleteId,
      menuCreateId, menuUpdateId, menuDeleteId,
      departmentListId, departmentCreateId, departmentUpdateId, departmentDeleteId
    ];

    // 关联角色和菜单
    if (adminRoleCode && menuIds.length > 0) {
      const roleMenus = menuIds.map(menuId => ({
        id: generateSnowflakeId(),
        role_code: adminRoleCode,
        menu_id: menuId,
        create_time: new Date(),
        update_time: new Date(),
      }));
      await queryInterface.bulkInsert('sys_role_menus', roleMenus);
    }
  },

  async down(queryInterface, Sequelize) {
    // 删除角色菜单关联数据
    await queryInterface.bulkDelete('sys_role_menus', null, {});
    // 删除菜单数据
    await queryInterface.bulkDelete('sys_menus', null, {});
  },
};